<?php
namespace app\tag\model;
use think\Cache;
use think\Db;
use think\Controller;

class Lists extends Controller
{
    
    public function getItemListByTag($tag,$listType)
    {
        $tag = json_decode($tag,true);
        $whereArray = [];
        foreach ($tag as $key => $val) {
            if (strpos($key, 'where') !== false) {
                if (strpos($val, '=') !== false) {
                    $tempVal = explode('=', $val);
                    $whereArray[trim($tempVal[0])] = trim($tempVal[1]);
                }
                if (strpos($val, '<>') !== false) {
                    $tempVal = explode('<>', $val);
                    $whereArray[trim($tempVal[0])] = ['<>',trim($tempVal[1])];
                    break;
                }
                if (strpos($val, 'like') !== false) {
                    $tempVal = explode('like', $val);
                    $whereArray[trim($tempVal[0])] = ['like',trim($tempVal[1])];
                }
                if (strpos($val, '>') !== false) {
                    $tempVal = explode('>', $val);
                    $whereArray[trim($tempVal[0])] = ['>',trim($tempVal[1])];
                }
                if (strpos($val, '<') !== false) {
                    $tempVal = explode('<', $val);
                    $whereArray[trim($tempVal[0])] = ['<',trim($tempVal[1])];
                }
            }
        }
        $tag['whereArray'] = $whereArray;
        
        if ($listType == 'list') {
            return $this->getItemList($tag);
        }
        if ($listType == 'pagination') {
            return $this->getPagination($tag);
        }
        
    }

    public function getItemList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'id';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
        $keywords = $paramData['keywords'] ? $paramData['keywords'] : null;
        $whereArray = $paramData['whereArray'] ? $paramData['whereArray'] : null;
        $itemIds = $paramData['itemIds'] ? $paramData['itemIds'] : null;
        $type = $paramData['type'] ? $paramData['type'] : null;
        $tagIds = $paramData['tagIds'] ? $paramData['tagIds'] : null;
        
		try{
	        if ($tagIds) {
	        	$paramData['type'] = 'article';
	            $itemArticleList = model('app\tag\model\TagsRel')->getItemListbyTagIds($paramData);                
	        } else {
	            $itemArticleList =  model('app\article\model\Articles')->getItemList($paramData);
	        }
	        if ($itemArticleList) {
	            foreach ($itemArticleList as $key => $value) {
	                   $itemArticleList[$key]['postType'] = 'article';
	            }
	        }
		} catch(\Exception $e) {}
		
		try{
	        if ($tagIds) {
	        	$paramData['type'] = 'post';
	            $itemPostList = model('app\tag\model\TagsRel')->getItemListbyTagIds($paramData);                
	        } else {
	            $itemPostList =  model('app\post\model\Posts')->getItemList($paramData);
	        }
	        if ($itemPostList) {
                foreach ($itemPostList as $key => $value) {
                	$itemPostList[$key]['postType'] = 'post';
                }
            }
		} catch(\Exception $e) {}
            
		try{
	        if ($tagIds) {
	        	$paramData['type'] = 'ask';
	            $itemAskList = model('app\tag\model\TagsRel')->getItemListbyTagIds($paramData);            
	        } else {
	            $itemAskList =  model('app\ask\model\Asks')->getItemList($paramData);
	        }
	        if ($itemAskList) {
	            foreach ($itemAskList as $key => $value) {
	            	$itemAskList[$key]['postType'] = 'ask';
	            }
	        }
		} catch(\Exception $e) {}
      	$itemArticleList = $itemArticleList ? $itemArticleList : [];
        $itemPostList = $itemPostList ? $itemPostList : [];
        $itemFeedList = $itemFeedList ? $itemFeedList : [];
        $itemAskList = $itemAskList ? $itemAskList : [];
        $itemAllList = array_merge($itemArticleList,$itemPostList,$itemFeedList,$itemAskList);
        if ($itemAllList) {
            array_multisort(array_column($itemAllList,'publish_time'),SORT_DESC,$itemAllList);
            $itemAllList = array_slice($itemAllList,0,10);
            if ($itemAllList) {
//              foreach ($itemAllList as $key => $value) {
//                  if ($itemAllList[$key]['postType'] == 'ask') {
//                      $itemAllList[$key] = model('app\ask\model\Asks')->getDescription(1);
//                  }
//                  if ($itemAllList[$key]['postType'] == 'post') {
//                      $itemAllList[$key] = model('app\post\model\Posts')->getDescription(1);
//                  }
//              }
            }
        }
        if ($itemAllList) {
            
        } else {
            $itemAllList = [];
        }
        return $itemAllList;
    }


}